package com.example.openim.utils;

import com.example.openim.enums.ErrorCode;
import lombok.Data;

import static com.example.openim.enums.ErrorCode.SUCCESS;


@Data
public class AjaxResult<T> {
    private boolean success;
    private String msg;
    private String code;
    private T data;

    public AjaxResult() {
    }

    public AjaxResult(T data) {
        this.success = true;
        this.data = data;
        this.code = SUCCESS.getCode();
        this.msg = SUCCESS.getMsg();
    }

    public AjaxResult(String code, String msg) {
        this.success = false;
        this.code = code;
        this.msg = msg;
    }

    public AjaxResult(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = true;
    }


    public static AjaxResult error(String code,String msg){
        return new AjaxResult(code,msg);
    }

    public static AjaxResult error(ErrorCode error){
        return new AjaxResult(error.getCode(),error.getMsg());
    }


    public static <T> AjaxResult success(T data){
        return new AjaxResult(SUCCESS.getCode(),SUCCESS.getMsg(),data);
    }
}
